package com.yc.pay.service.strategy.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.yc.pay.api.resp.NetBankPayData;
import com.yc.pay.api.resp.RespVo;
import com.yc.pay.config.YcPayConfig;
import com.yc.pay.api.enums.PayWayEnums;
import com.yc.pay.factory.PayWayFactory;
import com.yc.pay.service.strategy.IStrategyPay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * wap支付策略
 */
@Service
public class AlipayWapPayImpl implements IStrategyPay, InitializingBean {


    @Autowired
    private YcPayConfig ycPayConfig;

    private Logger logger = LoggerFactory.getLogger(AlipayWapPayImpl.class);


    @Override
    public RespVo pay() {

        Factory.setOptions(this.getOptions());
        try {
            AlipayTradeWapPayResponse response = Factory.Payment.Wap().pay("","","","","");
            if (StringUtils.isEmpty(response.getBody())){
                return RespVo.returnFailRespVo("支付宝返回的表为空");
            }
            NetBankPayData netBankPayData = new NetBankPayData();
            System.err.println(response.getBody());

            netBankPayData.setPayUrl(response.getBody());
            return RespVo.returnSuccessRespVo(netBankPayData);
        } catch (Exception e) {
            logger.error("Exception",e);
            return RespVo.returnFailRespVo(e.getMessage());
        }

    }


    /**
     * 初始化支付宝系统参数
     * @return
     */
    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = ycPayConfig.getServiceUrl();
        config.signType = "RSA2";

        config.appId =  ycPayConfig.getAppId(); //"<-- 请填写您的AppId，例如：2019091767145019 -->";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey =  ycPayConfig.getPivateKey();//"<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->";

        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey =  ycPayConfig.getAlipayPubKey(); //"<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";

        //可设置异步通知接收服务地址（可选）
        config.notifyUrl =  ycPayConfig.getNotifyUrl();//"<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
//        config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return config;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("----AlipayWapPayImpl---");
        PayWayFactory.concurrentHashMap.put(PayWayEnums.ALIPAY_WAP_PAY.getCode(),this);

    }
}
